﻿using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.ServiceLocation;
using System;

namespace Songhay.BiggestBox.Desktop.Modules.RestWeather.ViewModels
{
    using Portable.Shared.Models;
    using Portable.Shared.Services;

    public class NoaaWeatherViewModel : BindableBase
    {
        public NoaaWeatherViewModel()
        {
            this._restModelContextService = ServiceLocator.Current.GetInstance<IRestModelContextService>();
            this.LoadModelsAsync();
        }

        public bool IsDoingServiceOperation
        {
            get { return this._isDoingServiceOperation; }
            set { this.SetProperty(ref this._isDoingServiceOperation, value); }
        }

        public EarthToolsSunriseSunsetModel SunriseSunsetModel
        {
            get { return this._sunriseSunsetModel; }
            set { this.SetProperty(ref this._sunriseSunsetModel, value); }
        }

        public NoaaWeatherModel WeatherModel
        {
            get { return this._weatherModel; }
            set { this.SetProperty(ref this._weatherModel, value); }
        }

        async void LoadModelsAsync()
        {
            this.IsDoingServiceOperation = true;

            var href = string.Format("http://www.earthtools.org/sun/34.0561/118.4136/{0}/{1}/99/1", DateTime.Now.Day, DateTime.Now.Month);
            this.SunriseSunsetModel = await this._restModelContextService.GetEarthToolsSunriseSunsetModelAsync(new Uri(href));
            this.WeatherModel = await this._restModelContextService.GetNoaaWeatherModelAsync(new Uri("http://www.weather.gov/xml/current_obs/KLAX.xml", UriKind.Absolute));

            this.IsDoingServiceOperation = false;
        }

        readonly IRestModelContextService _restModelContextService;

        EarthToolsSunriseSunsetModel _sunriseSunsetModel;
        NoaaWeatherModel _weatherModel;

        bool _isDoingServiceOperation;
    }
}
